package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.i18n.model.SystemLanguageInfo;
import cn.b.sky.i18n.service.I18nService;
import cn.b.sky.user.service.MenuTranslationService;
import cn.b.sky.user.service.model.Menu;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/menuTranslation/*")
@Slf4j
public class MenuTranslationController extends SecurityController {

  @Autowired
  private MenuTranslationService menuTranslationService;

  @Autowired
  private I18nService i18nService;

  @RequestMapping("indexPage")
  public String menuTranslationPage(Model model, String navTabId, String menuCode, String sourceLanguage,
                                    String targetLanguage) {
    bindModel(model, navTabId, menuCode, sourceLanguage, targetLanguage);
    List<SystemLanguageInfo> languages = i18nService.listLanguage();
    model.addAttribute("languages", languages);
    return "menu/translationCondition";
  }

  @RequestMapping("listPage")
  public String menuTranslationListPage(Model model, String navTabId, String menuCode, String sourceLanguage,
                                        String targetLanguage) {
    bindModel(model, navTabId, menuCode, sourceLanguage, targetLanguage);
    List<SystemLanguageInfo> languages = i18nService.listLanguage();
    for (SystemLanguageInfo languageInfo : languages) {
      if (languageInfo.getLanguageCode().equals(sourceLanguage)) {
        model.addAttribute("sourceLanguageText", languageInfo.getLanguageName());
      }
      if (languageInfo.getLanguageCode().equals(targetLanguage)) {
        model.addAttribute("targetLanguageText", languageInfo.getLanguageName());
      }
    }
    return "menu/menuTranslation";
  }

  private void bindModel(Model model, String navTabId, String menuCode, String sourceLanguage, String targetLanguage) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("menuCode", menuCode);
    model.addAttribute("sourceLanguage", sourceLanguage);
    model.addAttribute("targetLanguage", targetLanguage);
  }

  @RequestMapping("getMenuList")
  @ResponseBody
  public DWZResult getMenuList(String menuCode, String sourceLanguage, String targetLanguage) {
    return ajaxDone(menuTranslationService.getMenuListByCode(menuCode, sourceLanguage, targetLanguage));
  }

  @RequestMapping("saveMenuList")
  @ResponseBody
  public DWZResult saveMenuList(String menus, String targetLanguage) {
    try {
      List<Menu> menuList = JSONArray.parseArray(menus, Menu.class);
      menuTranslationService.saveMenuList(menuList, targetLanguage);
      return ajaxDone(null);
    } catch (Exception e) {
      log.error("{}", e);
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.DATABASE_OPERATION_FAIL));
    }
  }

}
